package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.filechooser.FileSystemView;

import com.jidesoft.swing.JideButton;

import backend.ProgramLauncher;
/**
 * This class is the button used. It has an internal method for getting the icon from the path.
 * @author Itai Raccah
 *
 */
public class ShortcutButton extends JideButton implements Shortcut, ActionListener{
	private String execPath;
	private String group;
	public ShortcutButton() {
		super();
		
	}
	public void setup(String id, String execPath, String displayTitle, String group) {
		this.execPath = execPath;
		this.group = group;
		this.setIcon(getShortcutIcon());
		this.setText(displayTitle);
		this.addActionListener(this);
//		this.setFocusable(false);
		
	}
	private Icon getShortcutIcon() {
		File file = new File(execPath);
		try {
			Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
			return icon;
		} catch (Exception e) {
			return null;
		}
	    
		
	}

	public void actionPerformed(ActionEvent event) {
		ProgramLauncher.launch(execPath);
		System.out.println(event.getModifiers());
		if (event.getModifiers()==18){
			ShortcutFrame.closeOnFocusLoss = false;
			ShortcutFrame.getInstance().requestFocus();
			ShortcutFrame.getInstance().setAlwaysOnTop(true);
		}
			
		else
			System.exit(0);

	}
	public String getExecPath() {
		return execPath;
	}
	public AbstractButton getComponent() {
		return this;
	}

	
}
